package com;

import java.io.*;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/4/7 11:22
 * @description
 */
public class ObjectSteamDemo {
    //ObjectInputSteam 读入一个对象
    //ObjectOutpitSteam 写入一个对象
    public static void oosDemo() {
        try {
            FileOutputStream fos = new FileOutputStream("D:\\file\\test\\pig");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            Pig pig = new Pig(1, "花猪", 200);
            oos.writeObject(pig);
            oos.close();
        } catch (FileNotFoundException e) {
//            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
//            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public static void oisDemo() {
        try {
            FileInputStream fis = new FileInputStream(
                    "D:\\file\\test\\pig");
            ObjectInputStream ois = new ObjectInputStream(fis);

            Object obj = ois.readObject();
            Pig pig = (Pig) obj;
            int age = pig.getAge();
            String type = pig.getType();
            System.out.println("age = " + age);
            System.out.println("type = " + type);

            ois.close();
        } catch (FileNotFoundException e) {
//            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
//            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        oisDemo();
    }
}
